package ne.aishuyuan.video.utils;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
public class RestTemplateUtil {
    private static RestTemplate restTemplate;
    public static ResponseEntity<String> sendFormPostEntity(String url, HttpMethod httpMethod,
                                                            Map<String, Object> paramsMap,RestTemplate restTemplate) {
        HttpHeaders httpHeaders = new HttpHeaders();
        StringBuffer urlStringBuffer = new StringBuffer(url);
        HttpEntity<Object> httpEntity = null;
        urlStringBuffer.append("?");
        int index = 0;
        for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
            if (index == 0) {
                urlStringBuffer.append(entry.getKey()).append("=").append(entry.getValue());
            } else {
                urlStringBuffer.append("&").append(entry.getKey()).append("=").append(entry.getValue());
            }
            index++;
        }
        httpEntity = new HttpEntity<Object>(httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(urlStringBuffer.toString(), httpMethod,
                httpEntity, String.class);
        return responseEntity;
    }
}
